package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.UnionHuaBenAttackDamageData;
import com.yanqu.road.entity.union.huaben.UnionHuaBenUserAttack;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHuaBenAttackDamageDataDaoImpl extends TempDataObjectDao<UnionHuaBenAttackDamageData> {

    public static String getUnionHuaBenAttackDamageDataSql = "select * from `t_c_hua_ben_attack_damage` where `weekId`=? or `weekId` =?";
    public Map<Integer, Map<Long, Map<String, Map<Integer, UnionHuaBenAttackDamageData>>>> getUnionHuaBenAttackDamageData(int thisWeedId, int preWeedId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, Map<String, Map<Integer, UnionHuaBenAttackDamageData>>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHuaBenAttackDamageDataSql);
            int i = 1;
            call.setInt(i++, thisWeedId);
            call.setInt(i++, preWeedId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenAttackDamageData data = parse(rs);
                if(result.get(data.getWeekId()) == null){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                Map<Long, Map<String, Map<Integer, UnionHuaBenAttackDamageData>>> weekMap = result.get(data.getWeekId());

                if(weekMap.get(data.getServerId()) == null){
                    weekMap.put(data.getServerId(), new ConcurrentHashMap<>());
                }
                Map<String, Map<Integer, UnionHuaBenAttackDamageData>> serverMap = weekMap.get(data.getServerId());
                if(serverMap.get(data.getUnionUid()) == null){
                    serverMap.put(data.getUnionUid(), new ConcurrentHashMap<>());
                }
                Map<Integer, UnionHuaBenAttackDamageData> map = serverMap.get(data.getUnionUid());
                map.put(data.getLevel(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenAttackDamageDataSql = "insert into `t_c_hua_ben_attack_damage`(weekId,unionUid,serverId,level,damageMap) values (?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHuaBenAttackDamageData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDamageMap())));
        if (getDb().executeNoneQuery(insertUnionHuaBenAttackDamageDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenAttackDamageDataSql = "update `t_c_hua_ben_attack_damage` set serverId=?,damageMap=? where weekId=? and unionUid=? and level=?";

    @Override
    protected boolean updateData(UnionHuaBenAttackDamageData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDamageMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        if (getDb().executeNoneQuery(updateUnionHuaBenAttackDamageDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected UnionHuaBenAttackDamageData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenAttackDamageData data = new UnionHuaBenAttackDamageData();
        data.setWeekId(rs.getInt("weekId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setServerId(rs.getLong("serverId"));
        data.setLevel(rs.getInt("level"));
        data.setDamageMap(JSON.parseObject(rs.getString("damageMap"), new TypeReference<Map<Integer, List<UnionHuaBenUserAttack>>>() {}));

        return data;
    }
}
